﻿Public Class OkNgCell
    Private fIsCirCleCross As Boolean
    Private sngBtnWidth As Single = 7.5
    Private sngBtnCWidth As Single = 4
    Private btn As Button

    Public Enum Result
        OK
        NG
        Blank
    End Enum

    Public Property CircleCross As Boolean
        Get
            Return fIsCirCleCross
        End Get
        Set(ByVal value As Boolean)
            fIsCirCleCross = value
            If fIsCirCleCross Then
                btnOK.Text = "○"
                btnNG.Text = "×"
                sngBtnWidth = sngBtnCWidth
            Else
                btnOK.Text = "OK"
                btnNG.Text = "NG"
                sngBtnWidth = sngBtnWidth
            End If
            ToolTempH_FontChanged(Me, New EventArgs)
        End Set
    End Property

    Public Overrides Property Text As String
        Get
            If IsNothing(btn) Then
                Return btnNG.Text
            Else
                Return btn.Text
            End If
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property Value As Object
        Get
            If IsNothing(btn) Then
                Return Result.Blank
            Else
                If CBool(btnOK.Tag) Then
                    Return btnOK.Text
                ElseIf CBool(btnNG.Tag) Then
                    Return btnNG.Text
                Else
                    Return String.Empty
                End If
            End If
        End Get
        Set(ByVal value As Object)
            If IsNothing(value) Then
                Exit Property
            End If

            Select Case value.ToString
                Case OK
                    btnOK.BackColor = Color.Lime
                    btnOK.Tag = True.ToString
                    btnNG.BackColor = Drawing.SystemColors.Control
                    btnNG.UseVisualStyleBackColor = True
                    btnNG.Tag = False.ToString
                Case NG
                    btnNG.BackColor = Color.Lime
                    btnNG.Tag = True.ToString
                    btnOK.BackColor = Drawing.SystemColors.Control
                    btnOK.UseVisualStyleBackColor = True
                    btnOK.Tag = False.ToString
                Case Else
                    If IsNumeric(value) Then
                        Dim rslt As Result = CType(value, Result)
                        Select Case rslt
                            Case Result.OK
                                btnOK.BackColor = Color.Lime
                                btnOK.Tag = True.ToString
                                btnNG.BackColor = Drawing.SystemColors.Control
                                btnNG.UseVisualStyleBackColor = True
                                btnNG.Tag = False.ToString
                            Case Result.NG
                                btnNG.BackColor = Color.Lime
                                btnNG.Tag = True.ToString
                                btnOK.BackColor = Drawing.SystemColors.Control
                                btnOK.UseVisualStyleBackColor = True
                                btnOK.Tag = False.ToString
                            Case Result.Blank
                                Clear()
                            Case Else
                                Clear()
                        End Select
                    Else
                        Clear()
                    End If
            End Select

        End Set
    End Property

    Public Property btnWidth As Single
        Get
            Return sngBtnWidth
        End Get
        Set(ByVal value As Single)
            sngBtnWidth = value
            If Not fIsCirCleCross Then
                Me.btnOK.Width = Me.Font.Size * sngBtnWidth
                Me.btnNG.Width = Me.Font.Size * sngBtnWidth
                ToolTempH_SizeChanged(Me, New EventArgs)
            End If
        End Set
    End Property

    Public Property btnCWidth As Single
        Get
            Return sngBtnCWidth
        End Get
        Set(ByVal value As Single)
            sngBtnWidth = value
            If fIsCirCleCross Then
                Me.btnOK.Width = Me.Font.Size * sngBtnCWidth
                Me.btnNG.Width = Me.Font.Size * sngBtnCWidth
                ToolTempH_SizeChanged(Me, New EventArgs)
            End If
        End Set
    End Property

    Private Sub ToolTempH_FontChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FontChanged
        btnOK.Font = Me.Font
        btnNG.Font = Me.Font

        Me.btnOK.Height = Me.Font.Size * 2.5
        If fIsCirCleCross Then
            Me.btnOK.Width = Me.Font.Size * sngBtnCWidth
        Else
            Me.btnOK.Width = Me.Font.Size * sngBtnWidth
        End If

        Me.btnNG.Height = Me.Font.Size * 2.5
        If fIsCirCleCross Then
            Me.btnNG.Width = Me.Font.Size * sngBtnCWidth
        Else
            Me.btnNG.Width = Me.Font.Size * sngBtnWidth
        End If

        ToolTempH_SizeChanged(sender, e)
    End Sub

    Private Sub OkNgCell_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Clear()
    End Sub

    Private Sub ToolTempH_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged

        Me.btnOK.Width = Me.Font.Size * sngBtnWidth
        Me.btnNG.Width = Me.Font.Size * sngBtnWidth

        Me.btnOK.Top = (Me.Height - btnOK.Height) / 2
        Me.btnOK.Left = (Me.Width / 2 - btnOK.Width) / 2

        Me.btnNG.Top = (Me.Height - btnNG.Height) / 2
        Me.btnNG.Left = (Me.Width * 3 / 2 - btnOK.Width) / 2

        If Me.Width < btnOK.Width + btnNG.Width Then
            btnOK.Left = -1
            btnNG.Left = btnOK.Right - 1
            Me.btnOK.Width = Me.Font.Size * sngBtnWidth
            Me.btnNG.Width = Me.Font.Size * sngBtnWidth
            Me.Width = btnOK.Width + btnNG.Width - 1
        End If

        If Me.Height <= btnOK.Height Then
            btnOK.Top = -1
            btnNG.Top = -1
            Me.Height = btnOK.Height
        End If

    End Sub

    Protected Overridable Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        btn = CType(sender, Button)
        Dim fIsOK As Boolean = CBool(btn.Tag)

        If fIsOK Then
            btn.BackColor = Drawing.SystemColors.Control
            btn.UseVisualStyleBackColor = True
            btn.Tag = False.ToString
        Else
            btn.BackColor = Color.Lime
            btn.Tag = True.ToString
        End If

        btnNG.BackColor = Drawing.SystemColors.Control
        btnNG.UseVisualStyleBackColor = True
        btnNG.Tag = False.ToString

        MyBase.OnClick(New EventArgs)
    End Sub

    Protected Overridable Sub btnNG_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNG.Click
        btn = CType(sender, Button)
        Dim fIsOK As Boolean = CBool(btn.Tag)

        If fIsOK Then
            btn.BackColor = Drawing.SystemColors.Control
            btn.UseVisualStyleBackColor = True
            btn.Tag = False.ToString
        Else
            btn.BackColor = Color.Lime
            btn.Tag = True.ToString
        End If

        btnOK.BackColor = Drawing.SystemColors.Control
        btnOK.UseVisualStyleBackColor = True
        btnOK.Tag = False.ToString

        MyBase.OnClick(New EventArgs)
    End Sub

    Public Sub Clear()
        btnNG.BackColor = Drawing.SystemColors.Control
        btnNG.UseVisualStyleBackColor = True
        btnNG.Tag = False.ToString

        btnOK.BackColor = Drawing.SystemColors.Control
        btnOK.UseVisualStyleBackColor = True
        btnOK.Tag = False.ToString
    End Sub
End Class
